<?php

namespace app\common\model;

class SystemCity extends Base
{

    protected $name = "system_city";

    /**
     * @param string $province_id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getCityByProvinceList(string $province_id = ''): array
    {
        if (empty($province_id)) {
            return [];
        }
        $row = $this->field("id,city_id as value,city as name")
            ->where('father', $province_id)
            ->order('sort desc,id asc')
            ->select()
            ->toArray();
        $lists = [];
        if(!empty($row)){
            foreach ($row as $k=>$v){
                $lists[$k] = $v;
                $area = (new SystemArea())->getAreaByCityList($v['value']);
                $lists[$k]['children'] = $area ?? [];
            }
        }
        return $lists;
    }
}